package com.module.TraceSource.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import com.module.TraceSource.vo.TsEventRecord;

import base.dao.GenericHibernateDao;


public class TsEventRecordDao extends GenericHibernateDao<TsEventRecord,Long>{
	private static TsEventRecordDao dao;
	private TsEventRecordDao(){
		
	}
	public synchronized static TsEventRecordDao getInstance(){
		if(dao == null){
			dao = new TsEventRecordDao();
		}
		return dao;
	}
	
	
	public synchronized List<TsEventRecord> getTsEventRecord(Session session,Map<String,Object> map) {
		String hql = "";
		String entEventId = "";
		String sseq = "";
		int seq = 0;
		
		if(map!=null){
			entEventId = (String) map.get("eventId");
			sseq = (String) map.get("seq");
			seq = Integer.valueOf(sseq);
		}
		
		//按照顺序号排序
		hql = "from TsEventRecord as t where t.createtime=(select max(obj.createtime) from TsEventRecord obj where obj.status !='D' and obj.seq = '" + seq + "' and obj.sourceid = '" + entEventId + "' )";
				
		Query query = session.createQuery(hql.toString());
		
		List<TsEventRecord> tsEventRecords = query.list();
		
		return tsEventRecords;
		}
	
	
	public TsEventRecord findByPid(Session session,Long pid)throws SQLException{
		TsEventRecord vo = new TsEventRecord();
		Query query = session.createQuery(" from TsEventRecord where pId="+pid);
		List list=query.list();
		if(list!=null&&list.size()>0){
			vo=(TsEventRecord)list.get(0);
		}
		return vo;
	}
}
